import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export class FactoryMiddlewares implements NestMiddleware {

  constructor() { }

  use(req: Request, res: Response, next: NextFunction) {
    // 创建新的查询对象
    const modifiedQuery = { ...req.query };
    const modifiedBody = { ...req.query };
    // 修改参数
    if (modifiedQuery.factoryId === "-1") {
      modifiedQuery.factoryId = undefined;
      Object.defineProperty(req, 'query', {
        value: modifiedQuery,
        writable: true,
        enumerable: true,
        configurable: true
      });
    }
    if (modifiedBody.factoryId === '-1') {
      modifiedBody.factoryId = undefined;
      Object.defineProperty(req, 'body', {
        value: modifiedBody,
        writable: true,
        enumerable: true,
        configurable: true
      });
    }
    next();
  }
}